package com.d.ui.view.verinor.adapter;

import com.d.ui.view.ResourceTable;
import com.d.ui.view.verinor.models.VerInorModel;
import java.util.List;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

public class VerInorAdapter extends BaseItemProvider {
  private final List<VerInorModel> dataSet;
  private final Context mContext;

  public VerInorAdapter(Context mContext, List<VerInorModel> dataSet) {
    this.mContext = mContext;
    this.dataSet = dataSet;
  }

  @Override
  public int getCount() {
    return this.dataSet != null ? this.dataSet.size() : 0;
  }

  @Override
  public Object getItem(int i) {
    if (dataSet != null && i >= 0 && i < dataSet.size()) {
      return dataSet.get(i);
    }
    return null;
  }

  @Override
  public long getItemId(int i) {
    return i;
  }

  @Override
  public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
    Component childComponent = component;
    if (childComponent == null) {
      childComponent =
          LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_adapter_ver_inor_text,
              null, true);
    }
    VerInorModel verInorModel = dataSet.get(i);
    Text childText = (Text) childComponent.findComponentById(ResourceTable.Id_tv_desc);
    childText.setText(verInorModel.content);
    childComponent.setClickedListener(c -> {
      new ToastDialog(mContext).setText("Click at: " + verInorModel.content).setAlignment(LayoutAlignment.CENTER).show();
    });
    return childComponent;
  }
}
